package com.chookss.home.inspection;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.entity.InspectionPicEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.Utils;
import com.chookss.tools.bigImage.ImagePreview;
import com.chookss.tools.bigImage.bean.ImageInfo;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.Nullable;

public class InspectionAdapter extends BaseQuickAdapter<InspectionPicEntity.ImagePathListBean, BaseViewHolderEx> {
    private Activity context;
    private int width, height;
    private int type = 1;// 0需要特殊处理，1不用

    public InspectionAdapter(int layoutResId, Activity context, @Nullable List<InspectionPicEntity.ImagePathListBean> data, int type) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
        width = (Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 30)) / 4;
        height = width * 195 / 165;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, InspectionPicEntity.ImagePathListBean item) {
        ImageView ivBg = helper.getView(R.id.ivBg);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivBg.getLayoutParams();
        params.height = width;
        params.width = width;
        ivBg.setLayoutParams(params);
        if (type == 1) {
            GlideUtils.load(context, item.getImagePath(), ivBg, R.drawable.default_news);
        } else {
            if (getData().size() == 1) {
                GlideUtils.loadCustomRound(context, item.getImagePath(), ivBg, R.drawable.default_news, 15);
            } else if (getData().size() > 1 && getData().size() < 8) {
                if (helper.getItemPosition() == 0) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 4);
                } else if (helper.getItemPosition() == getData().size() - 1) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 5);
                } else {
                    GlideUtils.load(context, item.getImagePath(), ivBg, R.drawable.default_news);
                }
            } else if (getData().size() == 8) {
                if (helper.getItemPosition() == 0) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 0);
                } else if (helper.getItemPosition() == 3) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 1);
                } else if (helper.getItemPosition() == 4) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 2);
                } else if (helper.getItemPosition() == 7) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 3);
                } else {
                    GlideUtils.load(context, item.getImagePath(), ivBg, R.drawable.default_news);
                }
            } else {
                GlideUtils.load(context, item.getImagePath(), ivBg, R.drawable.default_news);
            }
        }


        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPicUrlList(helper.getItemPosition());
            }
        });
    }

    private void setPicUrlList(int position) {
        int current=position;
        List<ImageInfo> imageInfoList = new ArrayList<>();
        if (type == 1) {
            current=0;
            ImageInfo imageInfo = new ImageInfo();
            if (getData().get(position).getImagePath().contains("x-oss-process")) {
                imageInfo.setThumbnailUrl(getData().get(position).getImagePath().split("\\?")[0]);// 缩略图url
                imageInfo.setOriginUrl(getData().get(position).getImagePath().split("\\?")[0]);// 原图url
            } else {
                imageInfo.setOriginUrl(getData().get(position).getImagePath());// 原图url
                imageInfo.setThumbnailUrl(getData().get(position).getImagePath());// 缩略图url
            }
            imageInfoList.add(imageInfo);
        } else {
            for (int i = 0; i < getData().size(); i++) {
                ImageInfo imageInfo = new ImageInfo();
                if (getData().get(i).getImagePath().contains("x-oss-process")) {
                    imageInfo.setThumbnailUrl(getData().get(i).getImagePath().split("\\?")[0]);// 缩略图url
                    imageInfo.setOriginUrl(getData().get(i).getImagePath().split("\\?")[0]);// 原图url
                } else {
                    imageInfo.setOriginUrl(getData().get(i).getImagePath());// 原图url
                    imageInfo.setThumbnailUrl(getData().get(i).getImagePath());// 缩略图url
                }
                imageInfoList.add(imageInfo);
            }
        }
        ImagePreview
                .getInstance()
                // 上下文，必须是activity，不需要担心内存泄漏，本框架已经处理好；
                .setContext(context)
                // 设置从第几张开始看（索引从0开始）
                .setIndex(current)
                .setShowCloseButton(true)
                .setLoadStrategy(ImagePreview.LoadStrategy.NetworkAuto)
                .setImageInfoList(imageInfoList)
                .start();
    }
}
